package com.woniuxy.bases.service.impl;


import com.woniuxy.bases.entity.Area;
import com.woniuxy.bases.entity.dto.AreaUpdateDTO;
import com.woniuxy.bases.service.AreaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bases.entity.dto.GetAreaDTO;
import com.woniuxy.bases.entity.vo.AreaDelVo;
import com.woniuxy.bases.mapper.AreaMapper;
import com.woniuxy.comments.util.CustomPage;
import com.woniuxy.comments.util.ResponseResult;
import com.woniuxy.comments.util.ResponseState;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class AreaServiceImpl implements AreaService {
    @Resource
    private AreaMapper areaMapper;

    //分页获取全部的地区信息
    @Override
    public ResponseResult<CustomPage<List<Area>>> getAllArea(GetAreaDTO getAreaDTO) {
        PageHelper.startPage(getAreaDTO.getPage().getPageIndex(), getAreaDTO.getPage().getPageSize());
        List<Area> list = areaMapper.getAllArea(getAreaDTO.getAreaStatus());
        PageInfo<Area> pageInfo = new PageInfo<>(list);
        CustomPage<List<Area>> customPage = new CustomPage<>();
        customPage.setCurrentPage(pageInfo.getPageNum());
        customPage.setTotalPage(pageInfo.getPages());
        customPage.setTotal(pageInfo.getTotal());
        customPage.setSize(pageInfo.getSize());
        customPage.setData(list);
        return new ResponseResult<CustomPage<List<Area>>>()
                .setCode(200)
                .setMessage("查询成功")
                .setData(customPage)
                .setState(ResponseState.SELECT_SUCCESS);
    }

    @Transactional
    @Override
    public boolean banAreas(List<AreaDelVo> list) {
        return areaMapper.banAreas(list);
    }

    @Override
    public boolean addNewAreas(List<Area> list) {
        return areaMapper.addNewAreas(list);
    }

    @Override
    public boolean delAreas(List<AreaDelVo> list) {
        return areaMapper.delAreas(list);
    }

    @Override
    public boolean updateArea(AreaUpdateDTO areaUpdateDTO) {
        if (areaMapper.getAreaById(areaUpdateDTO.getAreaId())) {
            return areaMapper.updateArea(areaUpdateDTO);
        }
        return false;
    }


}
